iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 7
0
自我挑戰組

學習筆記系列 第 10

javap -v 測試(2)

  • 分享至 

  • xImage
  •  

javap -v指令紀錄

Java Tutorial

1

        try {
            Thread.sleep(1000L);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

結果:

    Code:
      stack=2, locals=2, args_size=1
         0: ldc2_w        #2                  // long 1000l
         3: invokestatic  #4                  // Method java/lang/Thread.sleep:(J)V
         6: goto          14
         9: astore_1
        10: aload_1
        11: invokevirtual #6                  // Method java/lang/InterruptedException.printStackTrace:()V
        14: return
      Exception table:
         from    to  target type
             0     6     9   Class java/lang/InterruptedException
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
           10       4     1     e   Ljava/lang/InterruptedException;
            0      15     0  args   [Ljava/lang/String;
      StackMapTable: number_of_entries = 2
}

2

int i = 0;
while (i < 5) {
   i++;
}

結果:

    Code:
      stack=2, locals=2, args_size=1
         0: iconst_0
         1: istore_1
         2: iload_1
         3: iconst_5
         4: if_icmpge     13
         7: iinc          1, 1
        10: goto          2
        13: return
      LineNumberTable:
        line 47: 0
        line 48: 2
        line 50: 7
        line 84: 13
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      14     0  args   [Ljava/lang/String;
            2      12     1     i   I
      StackMapTable: number_of_entries = 2
        frame_type = 252 /* append */
          offset_delta = 2
          locals = [ int ]
        frame_type = 10 /* same */
}

3

        for (int i = 0; i < 10; i++) {
            if (i == 4) {
                break;
            }
        }

結果:

    Code:
      stack=2, locals=2, args_size=1
         0: iconst_0    // i=0 的0
         1: istore_1    
         2: iload_1
         3: bipush        10      //  i<10 的10
         5: if_icmpge     22   // 如果 i > 10 的話到 22:
         8: iload_1
         9: iconst_4
        10: if_icmpne     16       // 這邊應該就是break,如果不等於4,到16:
        13: goto          22
        16: iinc          1, 1
        19: goto          2  # 在繼續迴圈
        22: return
      LineNumberTable:
        line 47: 0
        line 48: 8
        line 49: 13
        line 47: 16
        line 77: 22
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            2      20     1     i   I
            0      23     0  args   [Ljava/lang/String;
      StackMapTable: number_of_entries = 3
        frame_type = 252 /* append */
          offset_delta = 2
          locals = [ int ]
        frame_type = 13 /* same */
        frame_type = 250 /* chop */
          offset_delta = 5
}

4

        ArrayList test = new ArrayList();
        test.add("A");
        test.add(1);
        test.add(3);
        test.add(2.5);
        System.out.println(test);

結果:

    Code:
      stack=3, locals=2, args_size=1
         0: new           #2                  // class java/util/ArrayList
         3: dup
         4: invokespecial #3                  // Method java/util/ArrayList."<init>":()V
         7: astore_1
         8: aload_1
         9: ldc           #4                  // String A
        11: invokevirtual #5                  // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
        14: pop
        15: aload_1
        16: iconst_1
        17: invokestatic  #6                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
        20: invokevirtual #5                  // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
        23: pop
        24: aload_1
        25: iconst_3
        26: invokestatic  #6                  // Method java/lang/Integer.valueOf:(I)Ljava/lang/Integer;
        29: invokevirtual #5                  // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
        32: pop
        33: aload_1
        34: ldc2_w        #7                  // double 2.5d
        37: invokestatic  #9                  // Method java/lang/Double.valueOf:(D)Ljava/lang/Double;
        40: invokevirtual #5                  // Method java/util/ArrayList.add:(Ljava/lang/Object;)Z
        43: pop
        44: getstatic     #10                 // Field java/lang/System.out:Ljava/io/PrintStream;
        47: aload_1
        48: invokevirtual #11                 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
        51: return
      LineNumberTable:
        line 48: 0
        line 49: 8
        line 50: 15
        line 51: 24
        line 52: 33
        line 53: 44
        line 71: 51
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      52     0  args   [Ljava/lang/String;
            8      44     1  test   Ljava/util/ArrayList;
}

5

        HashMap<String, String> capitalCities = new HashMap<String, String>();
        capitalCities.put("England", "London");
        capitalCities.put("Germany", "Berlin");

結果:

    Code:
      stack=3, locals=2, args_size=1
         0: new           #2                  // class java/util/HashMap
         3: dup
         4: invokespecial #3                  // Method java/util/HashMap."<init>":()V
         7: astore_1
         8: aload_1
         9: ldc           #4                  // String England
        11: ldc           #5                  // String London
        13: invokevirtual #6                  // Method java/util/HashMap.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lan
g/Object;
        16: pop
        17: aload_1
        18: ldc           #7                  // String Germany
        20: ldc           #8                  // String Berlin
        22: invokevirtual #6                  // Method java/util/HashMap.put:(Ljava/lang/Object;Ljava/lang/Object;)Ljava/lan
g/Object;
        25: pop
        26: return
      LineNumberTable:
        line 49: 0
        line 50: 8
        line 51: 17
        line 60: 26
      LocalVariableTable:
        Start  Length  Slot  Name   Signature
            0      27     0  args   [Ljava/lang/String;
            8      19     1 capitalCities   Ljava/util/HashMap;
      LocalVariableTypeTable:
        Start  Length  Slot  Name   Signature
            8      19     1 capitalCities   Ljava/util/HashMap<Ljava/lang/String;Ljava/lang/String;>;
}


上一篇
javap -v 測試
下一篇
了解樹
系列文
學習筆記46
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言